package com.dwd.order.service.impl;

import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.basic.utils.CodeGenerateUtils;
import com.dwd.order.mapper.AdoptOrderMapper;
import com.dwd.order.mapper.OrderAddressMapper;
import com.dwd.order.mapper.UserAddressMapper;
import com.dwd.order.pojo.AdoptOrder;
import com.dwd.order.pojo.OrderAddress;
import com.dwd.order.pojo.UserAddress;
import com.dwd.order.service.IAdoptService;
import com.dwd.pet.mapper.PetMapper;
import com.dwd.pet.pojo.Pet;
import com.dwd.servicemanager.pojo.Product;
import com.dwd.user.mapper.UserMapper;
import com.dwd.user.pojo.LoginInfo;
import com.dwd.user.pojo.User;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/27 14:58
 * @Version 1.0
 */
@Service
public class AdoptServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptService {
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitOrder(Map<String, Long> map, LoginInfo loginInfo) {
        //查询对应的用户
        User user = userMapper.queryById(loginInfo.getId());
        Long addressId = map.get("addressId");
        //通过地址id查询用户地址
        UserAddress userAddress = userAddressMapper.queryById(addressId);
        //创建订单地址
        OrderAddress orderAddress = createOrderAddress(userAddress, user);
        //保存订单地址
        orderAddressMapper.save(orderAddress);
        //查询对应的pet
        Long petId = map.get("petId");
        Pet pet = petMapper.queryById(petId);
        //创建领养订单
        AdoptOrder adoptOrder = createAdoptOrder(pet, orderAddress, user);
        //保存领养订单
        adoptOrderMapper.save(adoptOrder);
    }


    /**
     * 创建宠物订单
     * @param pet
     * @param orderAddress
     * @param user
     * @return
     */
    private AdoptOrder createAdoptOrder(Pet pet, OrderAddress orderAddress, User user) {
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("领养" + pet.getName() + "宠物")
                .setPrice(new BigDecimal(pet.getCostprice().toString()))
                .setOrderSn(orderAddress.getOrderSn())
                .setLastPayTime(DateUtils.addMinutes(new Date(), 15))
                .setPet(pet)
                .setUser(user)
                .setShop(pet.getShop());
        return adoptOrder;
    }

    /**
     * 创建订单地址
     *
     * @param userAddress
     * @param user
     * @return
     */
    private OrderAddress createOrderAddress(UserAddress userAddress, User user) {
        OrderAddress orderAddress = new OrderAddress();
        //因为useraddress跟orderaddress字段相似度很高
        BeanUtils.copyProperties(userAddress, orderAddress);
        orderAddress.setCreateTime(new Date())
                .setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        return orderAddress;
    }


    /**
     * 根据id查询产品
     * @param id
     * @return
     */
    @Override
    public Product queryProduct(Long id) {
        return adoptOrderMapper.queryProduct(id);
    }
}
